from flask import Flask, render_template, request
import pymysql

app = Flask(__name__)
database = pymysql.connect(
    host='47.94.76.61',
    user='root',
    password='123456',
    db='ydzk',
    charset='utf8'
)
cur = database.cursor()
cid = None
cname = None
oid = None
current_brand = None


@app.route('/')
def show_login():
    global cid
    return render_template('login.html', cid=cid)


@app.route('/login', methods=['POST'])
def login():
    global cur, cid, cname
    username = request.form['username']
    password = request.form['password']
    cur.execute("SELECT * FROM c WHERE cID=%s AND pw=%s", [username, password])
    res = cur.fetchall()
    if len(res) == 1:
        cid = res[0][0]
        cname = res[0][1]
        return show_index()
    else:
        error = '用户名或密码错误，请重新输入！'
        return render_template('login.html', error=error)


@app.route('/show_signup')
def show_signup():
    global cur, cid
    cur.execute("SELECT cID FROM c ORDER BY cID ASC")
    customers = cur.fetchall()
    temp_cid = int(customers[len(customers) - 1][0]) + 1
    return render_template('login_.html', cid=cid, cname=cname, temp_cid=temp_cid)


@app.route('/signup')
def signup():
    global database, cur, cid, cname
    cid = request.args.get('userid')
    cname = request.args.get('username')
    age = 20
    gender = 'M'
    pw = request.args.get('password')
    address = request.args.get('address')
    cur.callproc('p_insert_c', args=(cid, cname, age, gender, pw, address))
    database.commit()
    return show_login()


@app.route('/logout')
def logout():
    global cid, cname, oid, current_brand
    cid = None
    cname = None
    oid = None
    current_brand = None
    return show_login()


@app.route('/index')
def show_index():
    global cid
    return render_template('home.html', cid=cid, cname=cname)


@app.route('/show_fault_history')
def show_fault_history():
    global cid
    return render_template('fault_history.html', cid=cid, cname=cname)


@app.route('/show_fault_prediction')
def show_fault_prediction():
    global cid
    return render_template('fault_prediction.html', cid=cid, cname=cname)


@app.route('/show_detect_online')
def show_detect_online():
    global cid
    return render_template('detect_online.html', cid=cid, cname=cname)


@app.route('/show_realtime_diagnostics')
def show_realtime_diagnostics():
    global cid
    return render_template('realtime_diagnostics.html', cid=cid, cname=cname)


@app.route('/show_protect_status')
def show_protect_status():
    global cid
    return render_template('protect_status.html', cid=cid, cname=cname)


@app.route('/show_fault_feature')
def show_fault_feature():
    global cid
    return render_template('fault_feature.html', cid=cid, cname=cname)


@app.route('/show_recorder_data')
def show_recorder_data():
    global cid
    return render_template('recorder_data.html', cid=cid, cname=cname)


if __name__ == '__main__':
    app.run(threaded=True, debug=True)
